package com.qq2008.game.mojie.server;

import com.qq2008.game.mojie.jdo.Monster;
import com.qq2008.game.mojie.jdo.Room;
import com.qq2008.game.mojie.service.NpcService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * NPC管理器
 */
@Component
@Order(value = 12)
public class NpcServer implements CommandLineRunner {
    @Resource
    private NpcService _npcService;
    // 所有存活NPC
    private static Map<Integer, Monster> npcCache;
    //
    private  static NpcService npcService;

    @PostConstruct
    public void init(){
        npcService = _npcService;
    }

    // 获取NPC信息
    public static Monster getNpc(int npcId) {
        return npcCache.get(npcId);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("= 启动怪物管理器");
        npcCache = new HashMap<>();

        System.out.println("λ 正在载入 怪物配置 设定...");
        List<Monster> npcList = npcService.list(null);
        npcList.forEach((monster) -> {
            monster.setMaxHP(monster.getHp());
            monster.setMaxMP(monster.getMp());
            Room room = SceneServer.getRoom(monster.getRoomId());
            if (null == room) return;
            // 加入到地图
            Map<Integer, Monster> roomNpcMap = room.getNpcMap();
            roomNpcMap.put(monster.getId(), monster);
            roomNpcMap.put(monster.getId(), monster);
            room.setNpcMap(roomNpcMap);
            //
            npcCache.put(monster.getId(), monster);
        });
        System.out.println("  怪物配置名單 " + npcCache.size() + " 条");
    }
}
